package com.zenithmind.library.config;

import com.zenithmind.library.service.*;
import com.zenithmind.library.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 图书馆服务配置类
 * 遵循依赖倒置原则：通过配置类管理Bean的创建和依赖关系
 * 遵循单一职责原则：只负责服务Bean的配置
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Slf4j
@Configuration
public class LibraryServiceConfiguration {

    /**
     * 配置作者查询服务
     * 如果没有其他实现，使用默认的AuthorServiceImpl
     */
    @Bean
    @ConditionalOnMissingBean(AuthorQueryService.class)
    public AuthorQueryService authorQueryService(AuthorService authorService) {
        log.info("配置作者查询服务");
        return authorService;
    }

    /**
     * 配置作者管理服务
     * 如果没有其他实现，使用默认的AuthorServiceImpl
     */
    @Bean
    @ConditionalOnMissingBean(AuthorManagementService.class)
    public AuthorManagementService authorManagementService(AuthorService authorService) {
        log.info("配置作者管理服务");
        return authorService;
    }

    /**
     * 配置作者统计服务
     * 如果没有其他实现，使用默认的AuthorServiceImpl
     */
    @Bean
    @ConditionalOnMissingBean(AuthorStatisticsService.class)
    public AuthorStatisticsService authorStatisticsService(AuthorService authorService) {
        log.info("配置作者统计服务");
        return authorService;
    }

    /**
     * 配置作者导入导出服务
     * 如果没有其他实现，使用默认的AuthorServiceImpl
     */
    @Bean
    @ConditionalOnMissingBean(AuthorImportExportService.class)
    public AuthorImportExportService authorImportExportService(AuthorService authorService) {
        log.info("配置作者导入导出服务");
        return authorService;
    }

    /**
     * 配置图书查询服务
     * 如果没有其他实现，使用默认的BookServiceImpl
     */
    @Bean
    @ConditionalOnMissingBean(BookQueryService.class)
    public BookQueryService bookQueryService(BookService bookService) {
        log.info("配置图书查询服务");
        return bookService;
    }

    /**
     * 配置图书管理服务
     * 如果没有其他实现，使用默认的BookServiceImpl
     */
    @Bean
    @ConditionalOnMissingBean(BookManagementService.class)
    public BookManagementService bookManagementService(BookService bookService) {
        log.info("配置图书管理服务");
        return bookService;
    }

    /**
     * 配置图书库存服务
     * 如果没有其他实现，使用默认的BookServiceImpl
     */
    @Bean
    @ConditionalOnMissingBean(BookInventoryService.class)
    public BookInventoryService bookInventoryService(BookService bookService) {
        log.info("配置图书库存服务");
        return bookService;
    }

    /**
     * 配置图书统计服务
     * 如果没有其他实现，使用默认的BookServiceImpl
     */
    @Bean
    @ConditionalOnMissingBean(BookStatisticsService.class)
    public BookStatisticsService bookStatisticsService(BookService bookService) {
        log.info("配置图书统计服务");
        return bookService;
    }

    /**
     * 注意：AuthorService和BookService的实现由Spring自动注入
     * 这里不需要手动创建Bean，因为AuthorServiceImpl和BookServiceImpl
     * 已经使用@Service注解标记，Spring会自动创建实例
     */

    /**
     * 配置服务适配器
     * 为其他服务提供兼容性接口
     */
    @Bean
    @ConditionalOnMissingBean
    public com.zenithmind.library.adapter.LibraryServiceAdapter libraryServiceAdapter(
            AuthorQueryService authorQueryService,
            AuthorManagementService authorManagementService,
            AuthorStatisticsService authorStatisticsService,
            BookQueryService bookQueryService,
            BookManagementService bookManagementService,
            BookInventoryService bookInventoryService,
            BookStatisticsService bookStatisticsService) {
        log.info("配置图书馆服务适配器");
        return new com.zenithmind.library.adapter.LibraryServiceAdapter(
                authorQueryService,
                authorManagementService,
                authorStatisticsService,
                bookQueryService,
                bookManagementService,
                bookInventoryService,
                bookStatisticsService
        );
    }
}
